package cn.bjxy.shishaolong.request_answer.service;

import cn.bjxy.shishaolong.request_answer.entity.Report;
import cn.bjxy.shishaolong.request_answer.mapper.ReportMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 问答举报表 服务实现类
 * </p>
 *
 * @author 时少龙
 * @since 2019-04-12
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements IReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public IPage<Report> queryByCondition(IPage<Report> page, Report report) {
        List<Report> reportList = reportMapper.queryByCondition(page, report);
        return page.setRecords(reportList);
    }

    @Override
    public List<Report> queryByCondition(Report report) {
        return reportMapper.queryByCondition(report);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean remove(String ids) {
        Boolean flag = false;
        for (String id : ids.split(",")) {
            flag = this.update(new UpdateWrapper<Report>().lambda()
                    .set(Report::getDeletedAt, LocalDateTime.now())
                    .set(Report::getUpdatedAt, LocalDateTime.now())
                    .eq(Report::getTid, id));
        }
        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean handle(String ids, Integer handleType) {
        int update = 0;
        for (String id : ids.split(",")) {
            Report report = new Report();
            report.setUpdatedAt(LocalDateTime.now());
            if (handleType == 1) { // 撤销 执行删除操作 修改日期加了
                report.setDeletedAt(LocalDateTime.now());
                update = reportMapper.update(report,
                        new UpdateWrapper<Report>().lambda().eq(Report::getTid, id));
            } else {
                report.setStatus(2);
                update = reportMapper.update(report,
                        new UpdateWrapper<Report>().lambda().eq(Report::getTid, id));
            }
        }
        return update == 0 ? false : true;
    }
}
